package com.xs.ai.services;

import org.springframework.ai.chat.client.AdvisedRequest;
import org.springframework.ai.chat.client.RequestResponseAdvisor;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class LoggingAdvisor implements RequestResponseAdvisor {

    private final VectorStore vectorStore;

    // 构造函数注入
    public LoggingAdvisor(VectorStore vectorStore) {
        this.vectorStore = vectorStore;
    }

    @Override
    public AdvisedRequest adviseRequest(AdvisedRequest request, Map<String, Object> context) {
        System.out.println("Request:"+request);
        // 执行相似性搜索并记录检索到的内容
        SearchRequest searchRequest = SearchRequest.query(request.userText()).withTopK(5).withSimilarityThreshold(0.1);
        List<Document> documents = vectorStore.similaritySearch(searchRequest);

        System.out.println("Retrieved documents: " + documents.size());
        for (int i = 0; i < documents.size(); i++) {
            Document doc = documents.get(i);
            System.out.println("Document " + (i+1) + ": " + doc.getContent());
        }
        return request;
    }
}
